﻿#ifndef OBJECTMANAGER_H
#define OBJECTMANAGER_H

#include <QObject>
#include <qhash.h>
#include "ObjAutoInject.h"

#include "LogSaver.h"

class ObjectManager :  public QObject,public ObjAutoInject
{
  Q_OBJECT
private://property
  QHash<QString,QObject*> objMap;
  static ObjectManager* objectManager;
private://dependency
  LogSaver* logSaver=LogSaver::getLogSaver();
public:
  explicit ObjectManager(QObject *parent = nullptr);
  virtual ~ObjectManager();
  static ObjectManager* getObjectManager();
signals:
private:
  void objectCreation();
  void objectInjection();
  void objectInitialization();
  template<class A>void addToObjMap(A* a,QString objName=QString())
  {addToObjMap((QObject*)a,objName.isEmpty()?getClassNameFirstLow<A>():objName);}
  template<class A>void addToObjMap(QString objName=QString())
  {addToObjMap<A>(new A(),objName);}
  template<class A>A* getObjFromMap()
  {return ObjAutoInject::getObjFromMap<A>(&objMap);}
  template<class A>bool isMapContains()
  {return objMap.contains(getClassNameFirstLow<A>());}
  void removeFromObjMap(QObject* obj);
  void removeFromObjMap(QString objName);
  void addToObjMap(QObject* obj,QString objName);
};

#endif // OBJECTMANAGER_H
